﻿@using OBUCare.Web.Infrastructure
@using OBUCare.Core.BL
@using OBUCare.Core.DataAccess;
@using OBUCare.Web.Models;
@using OBUCare.Core.Common;
@{
    ObuDropDownListHlper ddlHelper = new ObuDropDownListHlper();

    var operatorInfo = UIUtility.GetCurrentUser();

    var menuNames = ViewData["menuName"];

    string AgentNo = UIUtility.getAgentByModule(operatorInfo, menuNames.ToString());

}
@using (Html.BeginForm("ExportDaily", "Report", FormMethod.Post, new { @id = "searchForm" }))
{

    <table>
        <tr>
            <td style="width: 20%; text-align: right">日期：
            </td>
            <td style="width: 20%; text-align: left">
                @Html.Telerik().DatePicker().Name("FromCreatedDate").HtmlAttributes(new { style = "width: 170px" }).OpenOnFocus(true).ShowButton(false).Value(DateTime.Now.ToShortDateString())
            </td>
            <td style="width: 20%; text-align: right">至：
            </td>
            <td style="width: 20%; text-align: left">
                @Html.Telerik().DatePicker().Name("ToCreatedDate").HtmlAttributes(new { style = "width: 170px" }).OpenOnFocus(true).ShowButton(false).Value(DateTime.Now.ToShortDateString())
            </td>
            <td style="width: 20%; text-align: right">操作员：
            </td>
            <td style="width: 20%; text-align: left">
                @Html.Telerik().DropDownList().Name("Operator").BindTo(ddlHelper.GetOPERATORSByAgentNo(menuNames, UIConstants.AllItems, AgentNo, false, new KeyValuePair<string, string>(UIConstants.AllItems, UIConstants.AllItems)))
            </td>
        </tr>
        <tr>
            <td align="right">营业厅类别：
            </td>
            <td align="left">
                @Html.Telerik().DropDownList().Name("AgentType").BindTo(ddlHelper.GetAgentTypes(UIConstants.AllItems, false, AgentNo, new KeyValuePair<string, string>(UIConstants.AllItems, UIConstants.AllItems))).CascadeTo("Agent")
            </td>
            <td align="right">营业厅：
            </td>
            <td align="left">
                @*                @Html.Telerik().DropDownList().Name("Agent").HtmlAttributes(new { @style = "Width:230px;" }).ClientEvents(events => events.OnLoad("agentonload")).SelectedIndex(AgentNo == "" ? 0 : 1)*@
                @Html.Telerik().DropDownList().Name("Agent").HtmlAttributes(new { @style = "Width:230px;" }).DataBinding(binding => binding.Ajax().Select("_GetAgent", "RepairOrders", new { AgentNo = AgentNo }))
            </td>
            <td style="text-align: right;">工号：
            </td>
            <td style="text-align: left">
                @Html.TextBox("txtOperator", "", new { @style = "width:50px;height:15px", @MaxLength = 6 })
                (六位工号)
            </td>
        </tr>
        <tr>
            <td colspan="4">&nbsp;
            </td>
            <td colspan="2" style="text-align: center">
                <input type="button" value="查询" id="btnSearch" />
                @Html.Hidden("searchSql", "", new { @id = "searchSql" })
                @Html.Hidden("sql", "")
                @Html.Hidden("agentNO", AgentNo, new { @id = "agentNO" })
                @Html.Hidden("menuNames", menuNames, new { @id = "menuNames" })
                @*      @if (AgentNo == "" &&  AgentInfo=="全部")
                {  <input type="button" value="导出" id="btnExport" />
                }
                else
                {*@
                <input type="button" value="打印" id="btnPrint" />
                @* }*@
            </td>
        </tr>
    </table>

}
@using (Html.BeginForm("PrintDaily", "Report", FormMethod.Post, new { @id = "ExportForm", target = "_blank" }))
{
    @Html.Hidden("DailySql", "", new { @id = "DailySql" })
    @Html.Hidden("StartDate", "", new { @id = "StartDate" })
    @Html.Hidden("EndDate", "", new { @id = "EndDate" })
    @Html.Hidden("agentNO", AgentNo, new { @id = "agentNO" })
    @Html.Hidden("menuNames", menuNames, new { @id = "menuNames" })
}
<script type="text/javascript">

    $().ready(function () {

        $('#btnSearch').click(function () {
            search();
        });
        if ("@AgentNo".length != 0) {
            //debugger;
            var agentType = $('#AgentType').data('tDropDownList');
            var transferBillStatus = $('#TransferBillStatus').data('tDropDownList');
            agentType.select(1); agentType.disable();
        }

        $("#btnPrint").click(function () {
            //debugger;
            if ("@AgentNo" == "" && $('#Agent').val() == "全部") {
                var form = $('#searchForm');
                form.submit();
                return true;
            }
            //debugger;
            var form = $("#searchForm");
            dataString = form.serialize();
            $.ajax({
                type: "POST",
                url: "/Report/DailySearch",
                data: dataString,
                cache: false,
                dataType: "json",
                success: function (data) {
                    if (data.Success) {
                        $('#DailySql').val(data.Sql);
                        $('#StartDate').val($('#FromCreatedDate').val());
                        $('#EndDate').val($('#ToCreatedDate').val());
                        var form = $("#ExportForm");
                        form.submit();
                    }
                    else {
                        alert('失败：' + data.ErrorMsg)
                        return false;
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    var err = XMLHttpRequest + "<br />" + textStatus + "<br />" + errorThrown;
                    alert(err);
                }
            });

        });
    });

    function agentonload(e) {
        var agent = $('#Agent').data('tDropDownList');
        $.post("/RepairOrders/_GetAgent", { "AgentType": "全部", "AgentNo": "@AgentNo" }, function (data) {
            agent.dataBind(data);
            if ("@AgentNo" != "") {
                agent.select(1);
            }
            else {
                agent.select(0);
            }
        })
    }

    function search() {

        var form = $("#searchForm");
        dataString = form.serialize();
        $.ajax({
            type: "POST",
            url: "/Report/DailySearch",
            data: dataString,
            cache: false,
            dataType: "json",
            success: function (data) {

                if (data.Success) {
                    //alert('成功');
                    //debugger;
                    var grid = $('#Grid').data('tGrid');
                    $('#searchSql').val(data.Sql);
                    grid.rebind();

                    var gridMoney = $('#GridMoney').data('tGrid');
                    gridMoney.rebind();
                    if (data.ErrorMsg != "") {
                        alert(data.ErrorMsg);
                    }
                }
                else {
                    alert('失败：' + data.ErrorMsg)
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                var err = XMLHttpRequest + "<br />" + textStatus + "<br />" + errorThrown;
                alert(err);
            }
        });
    }
</script>
@{
    ddlHelper.Dispose();    
}
